# Contributor: <xmingske@gmail.com>
# Contributor: Sascha Paunovic <azarus@posteo.net>
# Contributor: Patrycja Rosa <alpine@ptrcnull.me>
# Maintainer: Patrycja Rosa <alpine@ptrcnull.me>
pkgname=minetest
pkgver=5.5.1
pkgrel=0
pkgdesc="An infinite-world block sandbox game and a game engine"
url="https://www.minetest.net/"
# riscv64 blocked by luajit
arch="all !riscv64"
license="LGPL-2.1-or-later AND CC-BY-SA-3.0"
pkgusers="minetest"
pkggroups="minetest"
install="$pkgname-server.pre-install"
depends="$pkgname-common"
makedepends="
	bzip2-dev
	cmake
	curl-dev
	freetype-dev
	gettext-dev
	hiredis-dev
	libjpeg-turbo-dev
	libogg-dev
	libpng-dev
	libvorbis-dev
	luajit-dev
	mesa-dev
	openal-soft-dev
	openssl1.1-compat-dev
	samurai
	sqlite-dev
	zstd-dev
	"
subpackages="$pkgname-doc
	$pkgname-lang
	$pkgname-common::noarch
	$pkgname-server
	$pkgname-server-openrc:openrc
	"
_irrlichtver="1.9.0mt5"
source="https://github.com/minetest/minetest/archive/refs/tags/$pkgver/minetest-$pkgver.tar.gz
	https://github.com/minetest/irrlicht/archive/refs/tags/$_irrlichtver/irrlichtmt-$_irrlichtver.tar.gz
	minetest-server.confd
	minetest-server.initd
	0001-CMake-link-with-Intl-when-necessary.patch
	"

prepare() {
	default_prepare

	mv "$srcdir"/irrlicht-$_irrlichtver "$srcdir"/minetest-$pkgver/lib/irrlichtmt
}

build() {
	cmake -G Ninja -B build_client \
		-DCMAKE_BUILD_TYPE=Release \
		-DCUSTOM_BINDIR=/usr/bin \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCUSTOM_DOCDIR="/usr/share/doc/$pkgname" \
		-DCUSTOM_SHAREDIR="/usr/share/$pkgname" \
		-DBUILD_CLIENT=1 \
		-DBUILD_SERVER=0 \
		-DENABLE_REDIS=0 \
		-DENABLE_CURL=1 \
		-DRUN_IN_PLACE=0
	cmake --build build_client

	cmake -G Ninja -B build_server \
		-DCMAKE_BUILD_TYPE=Release \
		-DCUSTOM_BINDIR=/usr/bin \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCUSTOM_DOCDIR="/usr/share/doc/minetest" \
		-DCUSTOM_SHAREDIR="/usr/share/minetest" \
		-DBUILD_CLIENT=0 \
		-DBUILD_SERVER=1 \
		-DENABLE_REDIS=1 \
		-DENABLE_CURL=1 \
		-DRUN_IN_PLACE=0
	cmake --build build_server
}

package() {
	DESTDIR="$pkgdir" cmake --install build_client
}

common() {
	pkgdesc="Minetest files used by both client & server"
	depends=""

	install -o minetest -g minetest -d "$subpkgdir"/var/lib/minetest
	amove \
		usr/share/minetest/builtin \
		usr/share/minetest/games
}

server() {
	pkgdesc="Minetest server"
	depends="$pkgname-common"

	cd "$builddir"
	install -Dm644 minetest.conf.example \
		"$subpkgdir"/etc/minetest/minetest.conf
	install -Dm755 bin/minetestserver \
		-t "$subpkgdir"/usr/bin
}

openrc() {
	pkgdesc="Minetest server (OpenRC init scripts)"
	install_if="$pkgname-server openrc"

	install -Dm755 "$srcdir"/minetest-server.initd \
		"$subpkgdir"/etc/init.d/minetest-server
	install -Dm644 "$srcdir"/minetest-server.confd \
		"$subpkgdir"/etc/conf.d/minetest-server
}

sha512sums="
d430c665f9634d34e3c2341300ea294204687ab8fce8f5a895cde38be277cac02d83777924524950f22027ee0d4a43a21a1dace8b1fe467e40bb7d30095de570  minetest-5.5.1.tar.gz
53a6f7d72b9fc877683e4df4a434180759e946b1d4e375210dc8656e7aa244f675d7795c75f574bc17c5806ba47b9081ee97e94fe28f1dd08dfc34ce3f0005ad  irrlichtmt-1.9.0mt5.tar.gz
7bca17dc0bd59db9c07c160677498aa09297026104e922bf93b7629c274d0aebd226af60c5e82f49ffe3cc5c1e4448e3798b6c2a44144fc6eca4d3aaed3384bb  minetest-server.confd
c27aea87468538b48b5e0a66fa5cdde4fc67f3c00a0e57f359a2f044a31c5617d1b5f3dec72e015537ee3bf126bd1073878c0f4447de67db9be2c4cf21416dd7  minetest-server.initd
c815e6c9c5433e4fda9b8559b662086e18b14461e4fd8626b4d5178554a2423c9b243450845d49710825b627dede91de8411655adff7a960672c639735499533  0001-CMake-link-with-Intl-when-necessary.patch
"
